package com.woniu.immunologicalmechanism.controller;


import com.woniu.common.entity.Message;
import com.woniu.common.entity.OrderInfo;
import com.woniu.common.entity.ResponseResult;
import com.woniu.common.entity.User;
import com.woniu.immunologicalmechanism.feign.UserOpenfeign;
import com.woniu.immunologicalmechanism.service.ImmuneService;
import com.woniu.immunologicalmechanism.service.impl.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@CrossOrigin
@RefreshScope
@RestController
@RequestMapping("immune")
@Api(tags = "免疫机构核验接口")
public class ImmuneController {
    @Autowired
    ImmuneService immuneService;
    @Autowired
    UserOpenfeign userOpenfeign;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("aa")
    public  void cc() {
        //跟新缓存
        immuneService.update(0);
    }

    @ApiOperation("根据电话免疫核验查询")
    @GetMapping("tel")
    public ResponseResult<List<OrderInfo>> getorderinfoBytel(String tel) {
        ResponseResult result = new ResponseResult();
        List<OrderInfo> orderInfos = null;
        if (tel == null || "".equals(tel)) {
            result.setCode(500);
            result.setMessage("电话不能为空");
            return result;
        }

        try {
            orderInfos = immuneService.getorderBytel(tel);
            for (int i = 0; i < orderInfos.size(); i++) {
                User user = userOpenfeign.findUser(orderInfos.get(i).getUid());
                User user1 = (User) user;
                orderInfos.get(i).setUser(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(400);
            result.setMessage("服务繁忙，请等待...");
            return result;
        }
        result.setCode(200);
        if (orderInfos.size() == 0) {
            result.setMessage("联系电话错误");
            return result;
        }
        result.setData(orderInfos);
        result.setMessage("查询成功");
        return result;
    }

    @Cacheable(value ="orderinfos",key="#status" )
    @ApiOperation("根据状态核验查询")
    @GetMapping("status")
    public ResponseResult<List<OrderInfo>> getorderinfoBySatust(Integer status) {
     //   System.out.println(status);
        ResponseResult result = new ResponseResult();
        List<OrderInfo> orderInfos = null;


        try {
            //status=1查询核验的信息
            if (status == 1) {
                List<OrderInfo> orderInfos1 = immuneService.getorderinfosBystatus(1);
                orderInfos = immuneService.getorderinfosBystatus(2);
                orderInfos.addAll(orderInfos1);
                for (int i = 0; i < orderInfos.size(); i++) {
                    User user = userOpenfeign.findUser(orderInfos.get(i).getUid());
                    User user1 = (User) user;
                    orderInfos.get(i).setUser(user);
                }
            } else {
                //status=0查询未核验的信息
                orderInfos = immuneService.getorderinfosBystatus(status);
                for (int i = 0; i < orderInfos.size(); i++) {
                    User user = userOpenfeign.findUser(orderInfos.get(i).getUid());
                    User user1 = (User) user;
                    orderInfos.get(i).setUser(user);
                }
            }

        } catch (Exception e) {
          e.printStackTrace();
            result.setCode(400);
            result.setMessage("服务繁忙，请等待...");
            return result;
        }
        result.setCode(200);
        if (orderInfos.size() == 0) {
            result.setMessage("空空如也");
            return result;
        }
        result.setData(orderInfos);
        result.setMessage("查询成功");
        return result;
    }

    @ApiOperation("添加核验信息")
    @GetMapping("add")
    public ResponseResult<List<OrderInfo>> add( OrderInfo orderInfo) {
        ResponseResult result = new ResponseResult();
        if (orderInfo == null) {
            return ResultUtil.empty(result);
        }
        result= immuneService.addorderInfo(orderInfo);
        //跟新缓存
        immuneService.update(0);
        return result;
    }

    @ApiOperation("通过uid查询核验信息")
    @GetMapping("getdogbyuid")
    public ResponseResult<List<OrderInfo>> getdogbyuid( Integer uid) {


        return immuneService.getdogByUid(uid);
    }

    @ApiOperation("通过核验id查询核验信息")
    @GetMapping("getdogbyoid")
    public ResponseResult<OrderInfo> getdogByoid( int oid) {
       // System.out.println(oid);


        return immuneService.getdogByoid(oid);
    }

    @ApiOperation("通过核验id添加核验结果")
    @GetMapping("update")
    public ResponseResult update( int oid,int status,String cause) {
        System.out.println(cause);

        ResponseResult result = immuneService.updateorderInfostatus(oid, status,cause);

      //跟新缓存
       immuneService.update(1);
       immuneService.update(0);
       //uid
        ResponseResult<OrderInfo> orderInfoResponseResult = immuneService.getdogByoid(oid);
        int uid = orderInfoResponseResult.getData().getUid();
        //狗狗名
        String name=orderInfoResponseResult.getData().getDname();
        //给用户发消息
        Message message=new Message();
        message.setTitle("核验结果");
        message.setU_id(uid);
        message.setType(2);
        if (status==1){
           // userOpenfeign.insertMessage("核验结果","尊敬的用户你好，你的"+name+"免疫核验不通过",2,uid);
         message.setContent("尊敬的用户你好，你的"+name+"免疫核验不通过");
         userOpenfeign.insertMessage(message);
        }
        else {
            message.setContent("尊敬的用户你好，你的"+name+"免疫核验通过");
            userOpenfeign.insertMessage(message);
        }

        return result;
    }



}
